package com.party.mobile.web.controller.mall;

import com.party.authorization.annotation.Authorization;
import com.party.common.paging.Page;
import com.party.core.exception.BusinessException;
import com.party.core.model.mall.MallCategorySiftGoodsInput;
import com.party.core.model.mall.MallGoodsFavorite;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.system.TargetType;
import com.party.core.service.mall.IMallGoodsFavoriteService;
import com.party.core.service.member.IMemberGroupService;
import com.party.mobile.biz.currentUser.CurrentUserBizService;
import com.party.mobile.biz.mall.MallGoodsBizService;
import com.party.mobile.biz.mall.MallShowGoodsBizService;
import com.party.mobile.web.annotation.VisitCollect;
import com.party.mobile.web.dto.AjaxResult;
import com.party.mobile.web.dto.login.output.CurrentUser;
import com.party.mobile.web.dto.mall.output.MallSiftGoodsOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 商城商品控制器
 * Created by wei.li
 *
 * @date 2017/8/17 0017
 * @time 10:11
 */

@Controller
@RequestMapping(value = "mall/goods")
public class MallGoodsController {
    @Autowired
    private MallGoodsBizService mallGoodsBizService;
    @Autowired
    private CurrentUserBizService currentUserBizService;
    @Autowired
    private IMallGoodsFavoriteService mallGoodsFavoriteService;
    @Autowired
    private MallShowGoodsBizService mallShowGoodsBizService;
    @Autowired
    private IMemberGroupService memberGroupService;
    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 获取商品信息
     *
     * @param id: 商品id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "info", method = RequestMethod.GET)
    @VisitCollect(module = TargetType.MALL)
    public AjaxResult info(String id, HttpServletRequest request) {
        try {
            CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
            String memberId = currentUser == null ? "" : currentUser.getId();
            return mallGoodsBizService.info(id, memberId);
        } catch (BusinessException e) {
            return AjaxResult.error(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("商品信息异常——id：" + id, e);
            return AjaxResult.error(100, "商品信息异常");
        }
    }

    /**
     * 规格选择
     *
     * @param id 商品id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "specs", method = RequestMethod.GET)
    public AjaxResult specs(String id) {
        try {
            return mallGoodsBizService.specs(id);
        } catch (Exception e) {
            logger.error("规格选择异常", e);
            return AjaxResult.error(100, "规格选择异常");
        }
    }

    /**
     * 通过分类筛选的商品列表
     *
     * @param input
     * @param page
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @VisitCollect(module = TargetType.MALL)
    public AjaxResult search(MallCategorySiftGoodsInput input, Page page) {
        try {
            return mallGoodsBizService.search(input, page);
        } catch (Exception e) {
            logger.error("通过分类筛选的商品列表异常", e);
            return AjaxResult.error(100, "通过分类筛选的商品列表异常");
        }
    }

    /**
     * 加入收藏商品
     *
     * @param goodsId 商品id
     * @param request
     * @return
     */
    @ResponseBody
    @Authorization
    @RequestMapping(value = "addFavorite", method = RequestMethod.POST)
    public AjaxResult addFavorite(String goodsId, HttpServletRequest request) {
        try {
            CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
            mallGoodsBizService.addFavorite(currentUser.getId(), goodsId);
            return AjaxResult.success();
        } catch (BusinessException e) {
            return AjaxResult.success(e.getMessage());
        } catch (Exception e) {
            logger.error("收藏商品异常", e);
            return AjaxResult.error(110, "收藏商品异常");
        }
    }

    /**
     * 取消收藏商品
     *
     * @param goodsId 商品id
     * @param request
     * @return
     */
    @ResponseBody
    @Authorization
    @RequestMapping(value = "removeFavorite", method = RequestMethod.POST)
    public AjaxResult removeFavorite(String goodsId, HttpServletRequest request) {
        try {
            CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
            MallGoodsFavorite favorite = mallGoodsFavoriteService.getUnique(new MallGoodsFavorite(goodsId, currentUser.getId()));
            if (favorite == null) {
                throw new BusinessException("该商品没有被收藏");
            }
            mallGoodsFavoriteService.delete(favorite.getId());
            return AjaxResult.success();
        } catch (BusinessException e) {
            return AjaxResult.success(e.getMessage());
        } catch (Exception e) {
            logger.error("取消收藏商品异常", e);
            return AjaxResult.error(110, "取消收藏商品异常");
        }
    }

    /**
     * 收藏列表
     *
     * @param request
     * @param page    分页参数
     * @return
     */
    @ResponseBody
    @Authorization
    @RequestMapping(value = "favoriteList", method = RequestMethod.GET)
    @VisitCollect(module = TargetType.MALL)
    public AjaxResult favoriteList(HttpServletRequest request, Page page) {
        try {
            CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
            List<MallSiftGoodsOutput> goodsOutputs = mallGoodsBizService.getFavoriteList(currentUser.getId(), page);
            return AjaxResult.success(goodsOutputs, page);
        } catch (Exception e) {
            logger.error("收藏商品列表异常", e);
            return AjaxResult.error(100, "收藏商品列表异常");
        }
    }

    /**
     * 分销商品列表
     *
     * @param request
     * @param page
     * @return
     */
    @ResponseBody
    @Authorization
    @RequestMapping("getDistributorGoodsList")
    public AjaxResult getDistributorGoodsList(HttpServletRequest request, Page page) {
        try {
            CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
            String getPartnerId = memberGroupService.getPartnerId(currentUser.getId());
            List<MallSiftGoodsOutput> distributorGoodsList = mallShowGoodsBizService.getDistributorGoodsList(getPartnerId, page);
            return AjaxResult.success(distributorGoodsList, page);
        } catch (Exception e) {
            logger.error("获取分销商品列表异常", e);
            return AjaxResult.error(100, "获取分销商品列表异常");
        }
    }
}
